<?php
/**
 * 缴费通知消息模型
 * User: wuhao
 * Date: 2017/4/18
 * Time: 16:44
 */
namespace app\backend\model;

use think\Model;

class PayNotice extends Model
{
    private $_payNoticeList = 'z_pay_notice_list';
    private $_payNoticeTmp  = 'z_pay_notice_template';
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增缴费消息模板
     * @date   2017-04-19
     * @author wuhao
     *
     * @param  int      $mid   通知对象
     * @param  int      $tmpId  对应模板id
     * @param  int      $ordernum   合同编号
     * @param  int      $beginCycle  开始周期
     * @param  int      $endCycle    结束周期
     * @param  int      $mobile   通知人财务联系电话
     * @param  int      $reportTime  通知时间
     * @param  int      $priority     优先级
     * @return int      主键id
     */
    public function addTemplate($mid, $tmpId, $ordernum, $beginCycle, $endCycle, $mobile,  $reportTime, $priority)
    {
        if (!$mid || !$tmpId || !$beginCycle || !$endCycle || !$ordernum || !$mobile || !$reportTime || !$priority) {
            return false;
        }
        $payNoticeData = [
            'mid'           => $mid,
            'tmp_id'        => $tmpId,
            'ordernum'      => $ordernum,
            'begin_cycle'   => $beginCycle,
            'end_cycle'     => $endCycle,
            'mobile'        => $mobile,
            'report_time'   => $reportTime,
            'priority'      => $priority,
            'status'        => 0,
        ];
        return $this->setTable($this->_payNoticeTmp)->insertGetId($payNoticeData);
    }

    /**
     * 批量添加用户消息模板
     * @date   2017-04-19
     * @author wuhao
     *
     * @param  array  $data   数据
     * @return array
     */
    public function addAll(array $data)
    {
        if (!$data || !is_array($data)) {
            return false;
        }
        return $this->setTable($this->_payNoticeTmp)->insertAll($data);
    }

    /**
     * 根据是否已生成消息队列状态与通知时间获取用户模板列表
     * @date   2017-04-19
     * @author wuhao
     *
     * @param  int      $time         通知时间
     * @param  int      $status       是否已生成消息队列 0=否  1=是
     * @param  int      $page         页数
     * @param  int      $pageSize     每页个数
     *
     * @return array
     */
    public function getTemplate($time, $status = 0, $page=1, $pageSize=500)
    {
        if (!$time || !in_array($status, [0, 1])) {
            return false;
        }
        $where = [
            'report_time' => ['elt', $time],
            'status'      => $status,
        ];
        return $this->setTable($this->_payNoticeTmp)->where($where)->page($page, $pageSize)->select();
    }

    /**
     * 删除用户模板
     * @date   2017-04-20
     * @author wuhao
     *
     * @param  int      $ordernum     合同编号
     *
     * @return bool
     */
    public function deleteTemplate($ordernum)
    {
        if (!$ordernum) {
            return false;
        }
        $where = ['ordernum' => $ordernum];
        return $this->setTable($this->_payNoticeTmp)->where($where)->delete();
    }

    /**
     * 更新用户消息模板为已生成队列状态
     * @date   2017-04-20
     * @author wuhao
     *
     * @param   int|array  $id      消息模板id
     * @param   int        $status  生成状态  1=已生成
     * @return bool
     */
    public function updateTemplateStatus($id, $status = 1)
    {
        if (!$status || !$id || (!is_numeric($id) && !is_array($id))) {
            return false;
        }
        $this->setTable($this->_payNoticeTmp);
        if (is_array($id)) {
            $res = $this->where(['id' => ['in', $id]])->update(['status' => $status]);
        } else {
            $res = $this->where(['id' => $id])->update(['status' => $status]);
        }
        return $res;
    }

    /**
     * 批量添加用户催缴信息
     * @date   2017-04-20
     * @author wuhao
     *
     * @param  array  $data  数据
     *
     * @return bool
     */
    public function addNoticeList($data)
    {
        if (!$data) {
            return false;
        }
        return $this->setTable($this->_payNoticeList)->insertAll($data);
    }

    /**
     * 根据是否已发送消息队列状态与通知时间获取消息队列列表
     * @date   2017-04-19
     * @author wuhao
     *
     * @param  int      $time        通知时间
     * @param  int      $notifyMethod 通知方式 1=短信 2=邮件 3=系统弹窗
     * @param  int      $contractType 合同类型 1=租赁 2=托管
     * @param  int      $status      短信是否已发送 0=否  1=是
     * @param  int      $page        页数
     * @param  int      $pageSize    每页个数
     *
     * @return array
     */
    public function getNoticeList($time, $notifyMethod = 1, $contractType = 1, $status = 0, $page=1, $pageSize=500)
    {
        if (!$time || !in_array($status, [0, 1])|| !in_array($notifyMethod, [1,2,3]) || !in_array($notifyMethod, [1,2])) {
            return false;
        }
        $where = [
            'report_time'   => ['elt', $time],
            'contract_type' => $contractType,
            'notify_method' => $notifyMethod,
            'send_status'   => $status,
        ];
        return $this->setTable($this->_payNoticeList)->where($where)->page($page, $pageSize)->select();
    }

    /**
     * 更新用户消息模板为已生成队列状态
     * @date   2017-04-20
     * @author wuhao
     *
     * @param   int|array  $id      消息模板id
     * @param   int        $time    发送时间戳
     * @param   int        $status  生成状态  1=已发送
     *
     * @return bool
     */
    public function updateNoticeListStatus($id, $time, $status = 1)
    {
        if (!$time || !$status || !$id || (!is_numeric($id) && !is_array($id))) {
            return false;
        }
        $this->setTable($this->_payNoticeList);
        if (is_array($id)) {
            $res = $this->where(['id' => ['in', $id]])->update(['send_status' => $status, 'send_time' => $time]);
        } else {
            $res = $this->where(['id' => $id])->update(['send_status' => $status, 'send_time' => $time]);
        }
        return $res;
    }

    /**
     * 新增催缴记录
     * @date   2017-05-25
     * @author wuhao
     *
     * @return int
     */
    public function addNotice($mobile, $mid, $contractType, $ordernum, $beginCycle, $endCycle, $buildingId,  $messageType, $notifyMethod, $eventType, $sendType, $totalMoney,
                              $detail = '', $moneyDetail = '', $reportTime = 0, $priority = 1, $sendTime = 0,$sendStatus = 0)
    {
        if (!$mid  || !$totalMoney ||!$mobile) {
            return false;
        }
        $data = [
            'mid'           => $mid,
            'contract_type' => $contractType,
            'building_id'   => $buildingId,
            'ordernum'      => $ordernum,
            'begin_cycle'   => $beginCycle,
            'end_cycle'     => $endCycle,
            'message_type'  => $messageType,
            'event_type'    => $eventType,
            'notify_method' => $notifyMethod,
            'send_type'     => $sendType,
            'mobile'        => $mobile,
            'report_time'   => $reportTime,
            'priority'      => $priority,
            'total_money'   => $totalMoney,
            'detail'        => $detail,
            'money_detail'  => $moneyDetail,
            'send_time'     => $sendTime,
            'send_status'   => $sendStatus,
        ];
        $res = $this->setTable($this->_payNoticeList)->insertGetId($data);
        return $res;
    }

    /**
     * 获取重发短信对应的ordernum列表
     * @date   2017-06-14
     * @author wuhao
     *
     * @return array
     */
    public function getReSendList($begin, $end, $orderNumArr, $mobile, $notifyMethod, $field = '*', $page = 1, $pagSize = 15)
    {
        if (!$begin || !$end || !$orderNumArr) {
            return false;
        }
        if ($mobile) {
            $where['mobile'] = $mobile;
        }
        $where['ordernum'] = ['in', $orderNumArr];
        $where['notify_method'] = $notifyMethod;
        $where['send_status'] = 1;
        $where['send_time'] = ['between', [$begin, $end]];
        $res =  $this->table($this->_payNoticeList)->field($field)->where($where)->page($page, $pagSize)->select();
        if ($res) {
            $sum = $this->table($this->_payNoticeList)->where($where)->count();
            $total = ceil($sum / $pagSize);
            return [
                'total' => $total,
                'list'  => $res,
            ];
        } else {
            return [];
        }
    }

    /**
     * 获取列表
     * @date   2017-06-15
     * @author wuhao
     *
     * @return array
     */
    public function getListByWhere($where, $field = '*')
    {
        if (!$where || !$field) {
            return false;
        }

        $res = $this->table($this->_payNoticeList)->field($field)->where($where)->select();
        return $res ?:[];
    }

    /**
     * 获取模板列表
     * @date   2017-06-15
     * @author wuhao
     *
     * @return array
     */
    public function getTempByWhere($where, $field = '*')
    {
        if (!$where || !$field) {
            return false;
        }

        $res = $this->table($this->_payNoticeTmp)->field($field)->where($where)->select();
        return $res ?:[];
    }

    /**
     * 更新模板列表
     * @date   2017-06-15
     * @author wuhao
     *
     * @return array
     */
    public function updateTempByWhere($where, $data)
    {
        if (!$where || !$data) {
            return false;
        }

        $res = $this->table($this->_payNoticeTmp)->where($where)->update($data);
        return $res ?:[];
    }
}